package vaani.internal.application;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author sourcemorph
 */
public class DotDesktopIndexer {

    private static List<String> dotDesktopPath;
    private static List<Index> dotDesktopIndices;


    static {
        dotDesktopPath = new LinkedList<String>();
        dotDesktopPath.add("/usr/share/applications");
        dotDesktopPath.add("/opt/kde3/share/applications");
        dotDesktopIndices = new LinkedList<Index>();
        for(String s : dotDesktopPath) {
            hashFiles(new File(s));
        }
    }
    
    private static void hashFiles(File path) {
        File [] files = path.listFiles();
        for(File f : files) {
            if(f.isDirectory()) {
                hashFiles(f);
            }
            if(f.isFile()) {
                if(f.toString().endsWith(".desktop")) {
                    //System.out.println(f.toString());
                    BufferedReader reader = null;
                    try {
                        reader = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
                        if(! reader.equals(null)) {
                            //System.out.println("yes");
                            String newLine = null;
                            String name = null, genericName = null, comment = null;
                            while((newLine = reader.readLine()) != null) {
                                if(name == null && newLine.startsWith("Name=")) {
                                    name = newLine.substring(newLine.indexOf('=') + 1);
                                }
                                if(genericName == null && newLine.startsWith("GenericName=")) {
                                    genericName = newLine.substring(newLine.indexOf('=') + 1);
                                }
                                if(comment == null && newLine.startsWith("Comment=")) {
                                    //System.out.println(newLine);
                                    comment = newLine.substring(newLine.indexOf('=') + 1);
                                }
                                if(name != null && genericName != null && comment != null) {
                                    //System.out.println(name + " " + genericName + " " + comment);
                                    dotDesktopIndices.add(new Index(f, name.toLowerCase(), genericName.toLowerCase(), comment.toLowerCase()));
                                    break;
                                }
                            }
                        }
                        reader.close();
                    } catch(IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        }
    }

    public static List<Result> searchApplication(List<String> query) {
        List<Result> result = new LinkedList<Result>();
        for(Index index : dotDesktopIndices) {
            //System.out.println(index.getName());
            double temp;
            if((temp = index.match(query)) > 0.0) {
                result.add(new Result(index.getDotDesktopFile(), index.getName(), temp));
            }
        }
        if(result.size() == 0) {
            return null;
        }
        return result;
    }

/*trial
   public static void main(String [] args) {
       List<String> temp = new LinkedList<String>();
       temp.add("Word");
       //temp.add("Processor");
       List<Result> res = searchApplication(temp);
       if(res != null) {
           for(Result r : res) {
               System.out.println(r.getName());
           }
       }
       else {
           System.out.println("query returned no results");
       }
   }*/
}
